package com.zjitc.service.imp;

import com.sun.org.apache.regexp.internal.RE;
import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import com.zjitc.untils.RedisUtiils;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 14:34
 * @description:
 */

@Service
public class UserServiceImp implements UserService {

  @Autowired
  private UserDao userDao;

  @Override
  public ServerResponse<User> login(String username, String password) {
    int count = userDao.userNameCount(username);
    if (count != 1) {
      return ServerResponse.createError("user do not exist");
    }

    // Todo. password 需要加密
    User user = userDao.findByUserNameAndPassword(username, password);
    if (user == null) {
      return ServerResponse.createError("password error");
    }

    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> register(User user) {
    int count = userDao.userNameCount(user.getUsername());
    if (count == 1) {
      return ServerResponse.createError("用户已存在");
    }
    userDao.register(user);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> checkValid(String str) {
    int count = userDao.userNameCount(str);
    if (count == 1) {
      return ServerResponse.createError("用户已存在");
    }
    int count2 = userDao.checkEmail(str);
    if (count2 == 1) {
      return ServerResponse.createError("用户已存在");
    }
    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse<User> getUserInfo(String username) {
    User user = userDao.findByUserName(username);
    if (user == null) {
      return ServerResponse.createError("找不到当前用户");
    }
    user.setPassword(null);
    return ServerResponse.createSuccess(user);
  }


  @Override
  public ServerResponse<User> forgetGetQuestion(String username) {
    int count = userDao.userNameCount(username);
    if (count == 1) {
      String question = userDao.userQuestion(username);
      if (question != null) {
        User user = userDao.findByUserName(username);
        return ServerResponse.createSuccess(user.getQuestion());
      }
      return ServerResponse.createError("该用户未设置找回密码问题");
    }
    return ServerResponse.createError("此用户未注册");
  }

  @Override
  public ServerResponse<User> resetPassword(String passwordOld, String passwordNew, User user) {

    int count = userDao.checkPassword(passwordOld);
    if (count == 0) {
      return ServerResponse.createError("旧密码错误");
    }

    int count2 = userDao.updatePassword(user.getUsername(), passwordNew);
    if (count2 > 0) {
      return ServerResponse.createSuccess("密码更新成功");
    }
    return ServerResponse.createError("密码更新失败");
  }

  @Override
  public ServerResponse<User> updateInformation(String email, String phone, String question,
      String answer, User user) {
    int count = userDao.updateInformation(user.getUsername(), email, phone, question, answer);
    if (count > 0) {
      return ServerResponse.createSuccess("更新个人信息成功");
    }
    return ServerResponse.createError("更新个人信息失败");
  }

  @Override
  public ServerResponse<User> getInformation(String username) {
    User user = userDao.findByUserName(username);
    if (user == null) {
      return ServerResponse.createError("找不到当前用户");
    }
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> logout(User user) {
    if (user == null) {
      return ServerResponse.createSuccess("退出成功");
    } else {
      return ServerResponse.createError("退出失败");
    }
  }

  @Override
  public ServerResponse<User> checkQuestionAndAnswer(String username, String question,
      String answer) {
      int count = userDao.checkQuestion(username, question);
      if (count != 0) {
        int count2 = userDao.checkAnswer(username, answer);
        if (count2 != 0) {
          String token = String.valueOf(UUID.randomUUID());
          RedisUtiils.add(username, token);
          return ServerResponse.createSuccess(token);
        }
    }
    return ServerResponse.createError("问题答案错误");
  }



  @Override
  public ServerResponse<User> forgetResetPassword(String username, String passwordNew,String forgetToken) {

      String token = RedisUtiils.find(username);
      if (token.equals(forgetToken)){
        int count2 = userDao.updatePassword(username,passwordNew);
        if(count2 > 0){
          RedisUtiils.delete(username);
          return ServerResponse.createSuccess("修改密码操作成功");
        }
      }
    return ServerResponse.createError("修改密码操作失效");
  }
}
